package com.oldbook.manage.controller;

import com.oldbook.common.annotation.Log;
import com.oldbook.common.annotation.RequiresIntegrity;
import com.oldbook.common.core.controller.BaseController;
import com.oldbook.common.core.domain.AjaxResult;
import com.oldbook.common.core.page.TableDataInfo;
import com.oldbook.common.enums.BusinessType;
import com.oldbook.common.utils.SecurityUtils;
import com.oldbook.common.utils.poi.ExcelUtil;
import com.oldbook.manage.domain.CreditApply;
import com.oldbook.manage.service.ICreditApplyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 学分申请Controller
 *
 * @author Exiaoke
 * @date 2025-02-18
 */
@RestController
@RequestMapping("/manage/apply")
@Api(tags = "学分申请管理")
public class CreditApplyController extends BaseController {

    @Autowired
    private ICreditApplyService creditApplyService;

    /**
     * 分页查询学分申请列表
     * @param creditApply 查询条件对象，包含筛选条件
     * @return TableDataInfo 分页数据结果，包含列表数据和分页信息
     */
    @PreAuthorize("@ss.hasPermi('manage:apply:list')")
    @GetMapping("/list")
    @ApiOperation("查询学分申请列表")
    @RequiresIntegrity
    public TableDataInfo list(@ApiParam("学分申请对象") CreditApply creditApply) {
        startPage();
        List<CreditApply> list = creditApplyService.selectCreditApplyList(creditApply);
        return getDataTable(list);
    }

    /**
     * 导出学分申请数据到Excel
     * @param response HTTP响应对象，用于输出文件流
     * @param creditApply 查询条件对象，包含筛选条件
     */
    @PreAuthorize("@ss.hasPermi('manage:apply:export')")
    @Log(title = "学分申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出学分申请列表")
    @RequiresIntegrity
    public void export(HttpServletResponse response, @ApiParam("学分申请对象") CreditApply creditApply) {
        List<CreditApply> list = creditApplyService.selectCreditApplyList(creditApply);
        ExcelUtil<CreditApply> util = new ExcelUtil<CreditApply>(CreditApply.class);
        util.exportExcel(response, list, "学分申请数据");
    }

    /**
     * 根据ID获取学分申请详细信息
     * @param applyId 申请记录的唯一标识符
     * @return AjaxResult 包含详细信息的响应对象
     */
    @PreAuthorize("@ss.hasPermi('manage:apply:query')")
    @GetMapping(value = "/{applyId}")
    @ApiOperation("获取学分申请详细信息")
    @RequiresIntegrity
    public AjaxResult getInfo(@PathVariable("applyId") @ApiParam("applyId的值") Long applyId) {
        return success(creditApplyService.selectCreditApplyByApplyId(applyId));
    }

    /**
     * 新增学分申请
     * @param creditApply 要添加的学分申请对象
     * @return AjaxResult 操作结果
     */
    @PreAuthorize("@ss.hasPermi('manage:apply:add')")
    @Log(title = "学分申请", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增学分申请")
    @RequiresIntegrity
    public AjaxResult add(@RequestBody @ApiParam("要添加的学分申请对象") CreditApply creditApply) {
        //申请人获取当前登录用户
        creditApply.setStudentId(SecurityUtils.getLoginUser().getUser().getStudentId());
        return toAjax(creditApplyService.insertCreditApply(creditApply));
    }

    /**
     * 修改学分申请信息
     * @param creditApply 要修改的学分申请对象
     * @return AjaxResult 操作结果
     */
    @PreAuthorize("@ss.hasPermi('manage:apply:edit')")
    @Log(title = "学分申请", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改学分申请")
    @RequiresIntegrity
    public AjaxResult edit(@RequestBody @ApiParam("要修改的学分申请对象") CreditApply creditApply) {
        return toAjax(creditApplyService.updateCreditApply(creditApply));
    }

    /**
     * 删除学分申请记录
     * @param applyIds 要删除的申请ID数组
     * @return AjaxResult 操作结果
     */
    @PreAuthorize("@ss.hasPermi('manage:apply:remove')")
    @Log(title = "学分申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{applyIds}")
    @ApiOperation("删除学分申请")
    @RequiresIntegrity
    public AjaxResult remove(@PathVariable @ApiParam("要删除的applyId数组") Long[] applyIds) {
        return toAjax(creditApplyService.deleteCreditApplyByApplyIds(applyIds));
    }
    /***
     * 根据学号获取学分申请信息
     */
    @PreAuthorize("@ss.hasPermi('manage:apply:query')")
    @Log(title = "学分申请", businessType = BusinessType.OTHER)
    @GetMapping("/getApplyByStudentId/{studentId}")
    @ApiOperation("根据学号获取学分申请信息")
    public AjaxResult getCreditApplyByStudentId(@PathVariable @ApiParam("学号") String studentId) {
        return success(creditApplyService.selectCreditApplyByStudentId(studentId));
    }
}
